<?php
	
		/**
		* 用于查询公用 的信息
		*/
	
	class CommonController extends Controller{
		
		/**
		 * 查看规则配置文件
		 */
		 protected function getRule(){
		 	//规则文件
		 	$ruleFile = ROOT_PATH.'/Admin/Config/Rule.php'; 
		 	$rule = include($ruleFile);
//			var_dump($rule);
			return $rule;
		 }
		
		/**
		 * 计算等级
		 */
		 protected function getlevel($exp){
		 	$rules = $this->getRule();
		 	$str =1;
		 	switch(true){
				case $exp>=$rules['LEVEL10']:
					$str = 10;
					break;
				case $exp >=$rules['LEVEL9']:
					$str = 9;
					break;
				case $exp >=$rules['LEVEL8']:
					$str = 8;
					break;
				case $exp >=$rules['LEVEL7']:
					$str = 7;
					break;
				case $exp >=$rules['LEVEL6']:
					$str = 6;
					break;
				case $exp >=$rules['LEVEL5']:
					$str = 5;
					break;
				case $exp >=$rules['LEVEL4']:
					$str = 4;
					break;
				case $exp >=$rules['LEVEL3']:
					$str = 3;
					break;
				case $exp >=$rules['LEVEL2']:
					$str = 2;
					break;
				default:
					$str = 1;
					break;					
		 	}
			return $str;
		 }
		
		
		/**
		 * 返回所有的分类信息 包含子级
		 */
		 protected function getAllCate(){
		 	$cates = D('Category');
			$this->assign('cate',$cates->allCate());
			
		 }
		 
		 /**
		  * 返回头部所有数据
		  */
		 protected function getHeadData(){
		 	$this->getTopCate();
			$this->getAskCount();
		 } 
		
		/**
		 * 返回顶级分类信息
		 */
		 protected function getTopCate(){
		 	$topCates = D('Category');
			//遍历首顶层分类 
			$this->assign('topcate',$topCates->topCate());
		 }
		 
		 /**
		  * 查询所有问题的总数
		  */
		  protected function getAskCount(){
		  	$askCount = M('ask');
			$count = $askCount->query('SELECT count("asid") as count FROM hd_ask');
			$count = current($count)['count'];
			$this->assign('count', $count);
		  }
		
		 /**
		  * 查询当前登陆的用户信息
		  */
		  protected function getUserInfo($uid=null){
		  	if(session('uid')){
		  			if(is_null($uid)){
		  		$user = D('user')->getUserInfo();
				$user = current($user);
				$user['lv'] = $this->getlevel($user['exp']);
				$this->assign('userinfo',$user);
		 	 	}else{
		  			$user = D('user')->getUserInfo($uid);
					$this->assign('userinfo',current($user));
		 	 	}
		  	
				return current($user);			
		 		 }
		  	}
		  
			
		 /**
		  * 查找待解决的问题列表
		  */
		  public function waitSolve(){
		  	$solve = M('ask');
			$solveList = $solve->query('SELECT asid,content,time,solve,answer FROM hd_ask WHERE solve=0 ORDER BY time DESC LIMIT 8');
			$this->assign('solveList', $solveList);
		  }
		/**
		 * 查询悬赏问题列表
		 */
		 public function rewardList(){
		 	$reward = M('ask');
			$rewardList = $reward->query('SELECT asid,reward,content,time,solve,answer FROM hd_ask WHERE reward>0 AND solve=0 ORDER BY reward DESC LIMIT 8');
			$this->assign('rewardList', $rewardList);
		 }
		
		/**
		 * 同时返回头部所有数据和右边所有数据
		 */
		 public function getTopRight(){
		 	//返回用户当前登陆的用户信息
		 	$this->getUserInfo();
			//返回头部信息
			$this->getHeadData();
		 }
		 
		 /**
		  * 获得当前的开始时间戳和结果时间戳
		  */
		 protected function getTodayTime(){
		 	//时间戳
		 	$arr = array();
		 	//今天开始的时间戳
		 	$arr['start'] = mktime(0,0,0,date('m'),date('d'),date('Y'));
			//今天结果的时间戳			
			$arr['end'] = mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
			//最后将这个时间戳返回来
			return $arr;
			
		 }
		 
		/**
		 * 显示本日回答数最多的用户
		 */
		 public function todayMoreAnswer(){
		 	$arr = $this->getTodayTime();
		 	$reward = M('ask');
			$todaymore = current($reward->query("SELECT u.accept,exp,u.uid,username,answer,face,point,an.time,count(username) as todaycount FROM hd_user as u JOIN hd_answer as an ON u.uid=an.uid WHERE time >= {$arr['start']} AND time <= {$arr['end']} GROUP BY username ORDER BY todaycount DESC LIMIT 1"));
			if(!empty($todaymore)){
				$todaymore['lv'] = $this->getlevel($todaymore['exp']);
				$todaymore['cnl'] = round($todaymore['accept']/$todaymore['answer']*100).'%';
				$this->assign('todaymore',$todaymore);	
			}
			
		 }
		 
		/**
		 * 显示历史回答数最多的用户
		 */
		 public function historyMore(){
		 	$reward = M('ask');
			$history = current($reward->query("SELECT u.accept,exp,u.uid,username,answer,face,point,an.time,count(username) as todaycount FROM hd_user as u JOIN hd_answer as an ON u.uid=an.uid ORDER BY answer DESC LIMIT 1"));
			$history['lv'] = $this->getlevel($history['exp']);
			$history['cnl'] = round($history['accept']/$history['answer']*100).'%';
			$this->assign('history',$history);
		 }

		/**
		 * 采纳次数最多的用户 取10人
		 */
		 public function accept(){
		 	$reward = M('ask');
			$accept = $reward->query("SELECT uid,username,accept FROM hd_user ORDER BY accept DESC limit 10");
			$this->assign('accept',$accept);
		 }
		/**
		  * 连续获得当前分类 的PID再压入到数组中
		  */
		 protected function getCates($data,$cid){
		 	$arr = array();
			//当传入的是$cid为0的话直接返回
			if($cid==0){
				return $arr;
			}
			//遍历，这个数组  当cid等于传入的cid那么就将这组数据压入数组
			//再使用pid进查找相应的父类ID 再合并这个数组
			//最后将数组返回去就得到了 当前数组 的父级数组
			foreach($data as $v){
				if($v['cid']==$cid){
					$arr[] = $v;
					$arr = array_merge($this->getCates($data,$v['pid']),$arr);
				}
			}
			return $arr;			
		 }
		 
		 /**
		  * 计算采纳率
		  */
		  protected function cnl($answer,$accept){
		  	$round = $accept/$answer;
		  	$round = round($round,2)*100;
		  	return $round;
		  }
		
	}
